﻿/* ============================================================================== 
2  * NET Framework：4.0.30319.17929
3  * 创 建 者：JDD01-PC.jdd01.zhouwei 
4  * 创建日期：2014/1/25 14:34:37  
5  * 功能说明：
6  * 引用说明：
* ==============================================================================*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.IO;
using System.Xml;
using System.Data;
using System.Data.SQLite;
using Tools.Data;
namespace ConsoleApplication7
{
	public class TSavle
	{
		public static void Save() {
			string path = ConfigurationManager.AppSettings["savle"].ToString() + DateTime.Now.ToString("yyyy-MM-dd") + "\\"+ Environment.MachineName + "\\{0}\\" ;
			
			#region 2.xml
			XmlDocument dpcew = new XmlDocument();
			dpcew.Load(AppDomain.CurrentDomain.BaseDirectory + "\\config\\xml.xml");
			XmlNodeList list = dpcew.DocumentElement.SelectNodes("item");
			if (list!=null)
			{
				foreach (XmlNode item in list)
				{
					string filename = item.Attributes["path"].Value;
					
					#region sql
					XmlNodeList list2 = item.SelectNodes("sql");
					if (list2 != null)
					{
						string sql = "";
						foreach (XmlNode item2 in list2)
						{
							sql = item2.InnerText;// +" ; ";
							if (sql != "")
							{
								Console.WriteLine(item.Attributes["name"].Value+"  ...  "+Path.GetFileName(filename));
								DataSet ds = Read_DataSet(filename, sql);
								if (ds != null)
								{
									for (int ii = 0; ii < ds.Tables.Count; ii++)
									{
										DataTableExploer.DatatableToCSVFile(ds.Tables[ii],string.Format (path,item.Attributes["name"].Value)+item2.Attributes["name"].Value + ".txt");
									}
								}
							}
						
						}
					}
					#endregion
					
				}
				
			}
			#endregion

		}
		/// <summary>
		/// 
		/// </summary>
		/// <param name="path"></param>
		/// <param name="sql"></param>
		/// <returns></returns>
		static DataSet Read_DataSet(string path, string sql)
		{
			DataSet ds = new DataSet();
			try
			{
				SQLiteConnectionStringBuilder builder = new SQLiteConnectionStringBuilder();
				builder.DataSource = path;

				builder.PageSize = 4096;
				builder.UseUTF16Encoding = true;
				string connstring = builder.ConnectionString;
				ds = SQLiteHelper.ExecuteDataSet(connstring, new System.Data.SQLite.SQLiteCommand(sql));//"select * from sqlite_master"

			}
			catch (Exception ee)
			{
				Clog.Write(Path.GetFileName(path), path + ee.Message);
			}
			return ds;
		}
	}
}

